import Vue from "vue";
import VueRouter from "vue-router";
import Shop_S from "../views/Shop_S.vue";
import Index from "../views/Index.vue";
import Person from "../views/Person.vue";
import Login from "../views/Login.vue";
import Reg from "../views/Reg.vue";
import Order from "../views/Order.vue";
import Details from "../views/Details.vue";
import ShopCar from "../components/Shop_Car.vue";
import ShopBook from "../components/Shop_Book.vue";
import IndexContent from "@/components/IndexContent.vue";
import MyList from "@/components/MyList.vue";


Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Index",
    component: Index, redirect: '/indexContent', children: [
      { path: '/indexContent', component: IndexContent },
      { path: '/myList', component: MyList },
    ]
  },
  {
    path: "/shop_s",
    name: "Shop_S",
    component: Shop_S,
    meta: {
      requireAuth: true
    }, redirect: '/shopCar', children: [
      { path: '/shopCar', component: ShopCar },
      { path: '/shopBook', component: ShopBook },
    ]
  },
  {
    path: "/index",
    name: "Index",
    component: Index, redirect: '/indexContent', children: [
      { path: '/indexContent', name: "IndexContent", component: IndexContent },
      { path: '/myList', name: "MyList", component: MyList },
    ]
  },
  {
    path: "/person",
    name: "Person",
    component: Person,
    meta: {
      requireAuth: true
    }
  }, {
    path: "/reg",
    name: "Reg",
    component: Reg,
    meta: {
      requireAuth: true
    }
  }
  , {
    path: "/login",
    name: "Login",
    component: Login,
  }, {
    path: "/order",
    name: "Order",
    component: Order,
    meta: {
      requireAuth: true
    }
  },
  {
    path: "/details",
    name: "Details",
    component: Details,
    meta: {
      requireAuth: true
    }
  }

];

const router = new VueRouter({
  routes
});

export default router;

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    if (sessionStorage.getItem("token") !== "" || sessionStorage.getItem("token") !== null) {
      next()
    } else {
      next({ path: '/login' })
    }
  } else {
    next();
  }
})